// JavaScript Document
//DataTables = new Array();
var gnLastEventId = 0;
var DT_ERROR = gnLastEventId ++;
var DT_WARNING = gnLastEventId ++;
var DT_NOTICE = gnLastEventId++;
var DT_ELEMENT_PRELOAD = gnLastEventId ++;
var DT_TABLE_READY = gnLastEventId ++;
var DT_DATA_READY = gnLastEventId ++;
var DT_SEARCH_FORM_REQUESTED = gnLastEventId ++;
var DT_SEARCH_FORM_READY = gnLastEventId ++;
//var DT_ROW_CLICKED = gnLastEventId ++;
var DT_PAGER_READY = gnLastEventId ++;

function DataTable(TableID, XMLDefinitionFile, ObjectName)
{
	this.TableID = TableID;
	this.XMLDefinitionFile = XMLDefinitionFile;
	this.Criteria = new Array();
	this.ObjectName = ObjectName;
	this.settings = {
		TableID: TableID,
		TableDef: XMLDefinitionFile,
		ObjectName: ObjectName 
	};
	//event manager
    this.eventManager = new _eventManager();
	this.params = '';
	this.ServerPath = "/QuickerData/TableServer.php";
	this.PageSize = 20;

	/* register the known events */
    for (var i = 0; i < gnLastEventId; i++){
        this.registerEventID( i );
    }
};

/*
* Property Get/Sets
*/
DataTable.prototype.getRecordCount = function(element)
{
	me = this;
	$.get(this.ServerPath + "?" + this.params, $.extend(this.settings, {Action: "getRecordCount"}),
	function(html)
	{
//		alert(html);
		me.RecordCount = Math.floor(html-0);
	});
};

DataTable.prototype.setDataTableElement = function(element)
{
	this.DataTableElement = element;
};

// for Searching
DataTable.prototype.setFormElement = function(element)
{
	this.SearchFormElement = element;
};

/*
* Search form section
*/
DataTable.prototype.setSearchDisplayMode = function(DisplayMode)
{
	this.ShowSearchForm({SearchDisplayMode: DisplayMode}, 'changing mode');
};

DataTable.prototype.SubmitSearchForm = function(args, message)
{
	var thing = $("input", this.SearchFormElement).serialize();
	thing = thing + $("textarea", this.SearchFormElement).serialize();
	alert(thing);
	this.ShowDataTable(thing, 'Apply filter...');

	//alert(thing);
};

DataTable.prototype.ResetSearchForm = function(args, message)
{
	args = {ResetSearchForm: true};
	this.ShowSearchForm(args, 'changing mode');
	
};

/*
* Ajax functions
*/
DataTable.prototype.ShowSearchForm = function(args, message)
{
	var me = this;
	me.triggerEvent(DT_SEARCH_FORM_REQUESTED, this.SearchFormElement);
	message = (message) ? message : 'Loading...';
	settings = (args) ? $.extend(this.settings, args) : this.settings;
	this.SearchFormElement.html(message).load(this.ServerPath + "?" + this.params, $.extend(settings, {Action: "getSearchControlHTML"}),
	function()
	{
		me.triggerEvent(DT_SEARCH_FORM_READY, me.SearchFormElement);
	});	
};

DataTable.prototype.ShowDataTable = function(args, message)
{
	var me = this;
	var params = this.params;
	var settings = this.settings;
	this.getRecordCount();
//	me.triggerEvent(DT_ELEMENT_PRELOAD, this.DataTableElement);
	message = (message) ? message : 'Loading...';
	if(isObject(args))
	{
		settings = $.extend(this.settings, args);
	}
	if(isString(args))
	{
		params += '&' + args;
	}
//	$("table.DataTable", this.DataTableElement).remove('tr.DataTableRow');
	
	this.DataTableElement.html(message).load(this.ServerPath + "?" + params, $.extend(settings, {Action: "getDataTableHTML"}),
	function()
	{
		me.triggerEvent(DT_TABLE_READY, me.DataTableElement);
	});
};

DataTable.prototype.LoadDataRows = function(args, message)
{
	var me = this;
	var PageCount = Math.floor(me.RecordCount / me.PageSize) + 1;
//	me.triggerEvent(DT_ELEMENT_PRELOAD, this.DataTableElement);
	message = (message) ? message : 'Loading...';
	settings = (args) ? $.extend(this.settings, args) : this.settings;
//	alert('RecordCount: ' + me.RecordCount + ', PageSize: ' + me.PageSize + ', PageCount: ' + PageCount);
///*

	if(PageCount > 100) return;
	for(i = 1; i < PageCount; i++)
	{
		$.get(this.ServerPath + "?" + this.params, $.extend(settings, {Page:i, Action: "getDataRowsHTML"}),
		function(html)
		{
			$("table.DataTable", this.DataTableElement).append(html);
			me.triggerEvent(DT_DATA_READY, me.DataTableElement);
		});	
	}
//*/

};

DataTable.prototype.ToggleSort = function(column, direction)
{
	this.ShowDataTable({Sort: column, SortDirection:direction});
};

/*
* Event Manager, stolen from Kamap... thanks Kamap!
*/
DataTable.prototype.registerEventID = function( eventID ) {
    return this.eventManager.registerEventID(eventID);
};

DataTable.prototype.registerForEvent = function( eventID, obj, func ) {
    return this.eventManager.registerForEvent(eventID, obj, func);
};

DataTable.prototype.deregisterForEvent = function( eventID, obj, func ) {
    return this.eventManager.deregisterForEvent(eventID, obj, func);
};

DataTable.prototype.triggerEvent = function( eventID /*pass additional arguments*/ ) {
    return this.eventManager.triggerEvent.apply( this.eventManager, arguments );
};

//isObject(a)
//This function returns true if a is an object, array, or function. It returns false if a is a string, number, Boolean, null, or undefined.
function isObject(a)
{
	return (typeof a == 'object' && !!a) || isFunction(a);
};

//isString(a)
// This function returns true if a is a string. (but not a string object)
function isString(a)
{
	return typeof a == 'string';
};
function isFunction(a)
 {
      return typeof a == 'function';
 };